package com.dfiov.cloud.msmtools.mapper.entity.msinfofeedback;

import com.dfiov.cloud.msmtools.entity.dto.MsmtoolsColumnsDto;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Page;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;


@Setter
@Getter
@AllArgsConstructor
public class PageResult<T> {
    List<T> list;
    Pagination pagination;
    List<MsmtoolsColumnsDto> mapKey;

    public PageResult() {
        this.list = new ArrayList<>();
        this.pagination = new Pagination();
    }

    public static <T> PageResult<T> getPageVo(Page<T> page) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setList(page.getContent());
        pageResult.setPagination(new Pagination(page.getNumber() + 1, page.getSize(), page.getTotalElements()));
        return pageResult;
    }

    public static <T> PageResult<T> getPageVo(List<T> list, Page page) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setList(list);
        pageResult.setPagination(new Pagination(page.getNumber() + 1, page.getSize(), page.getTotalElements()));
        return pageResult;
    }

    public static <K, T> PageResult<T> getPageVo(Page<K> page, Function<K, T> function) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setPagination(new Pagination(page.getNumber() + 1, page.getSize(), page.getTotalElements()));
        if (!CollectionUtils.isEmpty(page.getContent())) {
            List<T> list = new ArrayList<>();
            for (K k : page.getContent()) {
                list.add(function.apply(k));
            }
            pageResult.setList(list);
        }
        return pageResult;
    }

    public static <K, T> PageResult<T> getPageVo(PageInfo<K> page, Function<K, T> function) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setPagination(new Pagination(page.getPageNum(), page.getPageSize(), page.getTotal()));
        List<T> list = new ArrayList<>();
        for (K k : page.getList()) {
            list.add(function.apply(k));
        }
        pageResult.setList(list);
        return pageResult;
    }

    public static <K> PageResult<K> getPageVo(PageInfo<K> page) {
        PageResult<K> pageResult = new PageResult<>();
        pageResult.setPagination(new Pagination(page.getPageNum(), page.getPageSize(), page.getTotal()));
        pageResult.setList(page.getList());
        return pageResult;
    }

}
